package com.tutorial.boot_demo.service;

import com.tutorial.boot_demo.converter.StudentConverter;
import com.tutorial.boot_demo.dao.Student;
import com.tutorial.boot_demo.dao.StudentRepository;
import com.tutorial.boot_demo.dto.StudentDTO;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    @Override
    public StudentDTO getStudentById(long id) {
        Student student = studentRepository.findById(id).orElseThrow(RuntimeException::new);
        return StudentConverter.convertStudent(student);
    }

    @Override
    public Long addNewStudent(StudentDTO studentDTO){
        List<Student> studentsList = studentRepository.findByEmail(studentDTO.getEmail());
        if(!CollectionUtils.isEmpty(studentsList)){
            throw new IllegalStateException("email: " + studentDTO.getEmail() + " already exist");
        }
        Student student = studentRepository.save(StudentConverter.convertStudent(studentDTO));
        return student.getId();
    }

    @Override
    public void deleteStudentById(long id){
        studentRepository.findById(id).orElseThrow(()->new IllegalStateException("id " + id + " student not found"));
        studentRepository.deleteById(id);
    }
    @Override
    @Transactional
    public StudentDTO updateStudentById(long id, String name, String email){
        Student studentInDB =  studentRepository.findById(id).orElseThrow(()->new IllegalArgumentException("id " +id +" student not found"));
        if (StringUtils.hasLength(name) && !studentInDB.getName().equals(name)){
            studentInDB.setName(name);
        }
        if (StringUtils.hasLength(email) && !studentInDB.getEmail().equals(email)){
            studentInDB.setEmail(email);
        }
        Student student = studentRepository.save(studentInDB);
        return  StudentConverter.convertStudent(student);
    }


}
